package com.qfedu.edu.utils;

import com.alibaba.fastjson.JSON;
import com.qfedu.edu.result.R;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xiaobobo
 * @title: HttpResponseUtils
 * @projectName seckill-fy-2401-parent
 * @description: TODO
 * @date 2024/11/4  15:41
 */
public class HttpResponseUtils {

  /**
   * 设置响应
   * @param response
   * @param error
   */
  public static void sendResponse(HttpServletResponse response, R error) {
        //设置编码
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(JSON.toJSONString(error));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            writer.flush();
            writer.close();
        }
    }

}
